package dymnd.net;

import java.awt.Color;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class PacketIO {
	
	private static final int TYPE_PAINT_PACKET = 0;
	private static final int TYPE_CHAT_PACKET = 1;
	private static final int TYPE_QUERY_PACKET = 2;
	
	
	public static final void writePacket(BasePacket p, OutputStream o)	{
		
		DataOutputStream out = new DataOutputStream(o);
		
		try	{
			if(p instanceof PaintPacket)	{
				//tell the listening end that the following packet is a PaintPacket
				out.writeInt(TYPE_PAINT_PACKET);
				
				//begin packet content
				
				//action
				out.writeInt(((PaintPacket) p).action);
				//owner hash
				out.writeInt(p.ownerHash);
				//plugin hash
				out.writeInt(((PaintPacket) p).pluginHash);
				//x, y
				out.writeInt(((PaintPacket) p).x);
				out.writeInt(((PaintPacket) p).y);
				
				//lastx, lasty
				out.writeInt(((PaintPacket) p).lastX);
				out.writeInt(((PaintPacket) p).lastY);
				
				//layer
				out.writeInt(((PaintPacket) p).layer);
				
				//pressure
				out.writeDouble(((PaintPacket) p).pressure);
				
				//radius
				out.writeInt(((PaintPacket) p).rad);
				
				//stroke start
				out.writeBoolean(((PaintPacket) p).strokeStart);
				
				//whether input has been released or not
				out.writeBoolean(((PaintPacket) p).inputReleased);
				
				//the alpha of the current operation
				out.writeInt(((PaintPacket) p).alpha);
				
				//primary color
				out.writeInt(((PaintPacket) p).primaryColor.getRGB());
				
				//secondary color
				out.writeInt(((PaintPacket) p).secondaryColor.getRGB());
				
				
			}	else if(p instanceof QueryPacket)	{
				
				out.writeInt(TYPE_QUERY_PACKET);
				
				out.writeInt(p.ownerHash);
				out.writeInt(((QueryPacket) p).imageWidth);
				out.writeInt(((QueryPacket) p).imageHeight);
				out.writeInt(((QueryPacket) p).layerCount);
				out.writeInt(((QueryPacket) p).permissions);
				out.writeInt(((QueryPacket) p).queryID);
				
				char[] username;
				if(((QueryPacket) p).username != null)
					username = ((QueryPacket) p).username.toCharArray();
				else
					username = new char[0];
				//write the username out here 
				out.writeInt(username.length);
				for(int i = 0; i < username.length; i++)	
					out.writeChar(username[i]);
				
				
				
				
			}
			
		}	catch(Exception x)	{
			x.printStackTrace();
		}
		
	}
	
	public static final BasePacket readPacket(InputStream i)	{
		
		DataInputStream dis = new DataInputStream(i);
		try	{
			int packetType = dis.readInt();
			
			switch(dis.readInt())	{
			case TYPE_PAINT_PACKET:
				PaintPacket p = new PaintPacket();
				
				p.action = dis.readInt();
				
				p.ownerHash = dis.readInt();
				p.pluginHash = dis.readInt();
				
				p.x = dis.readInt();
				p.y = dis.readInt();
				
				p.lastX = dis.readInt();
				p.lastY = dis.readInt();
				
				p.layer = dis.readInt();
				
				p.pressure = dis.readDouble();
				
				p.rad = dis.readInt();
				
				p.strokeStart = dis.readBoolean();
				
				p.inputReleased = dis.readBoolean();
				
				p.alpha = dis.readInt();
				
				//primary color values
				p.primaryColor = new Color(dis.readInt());
				
				//secondary color
				p.secondaryColor = new Color(dis.readInt());
				
				
				return p;
				
			case TYPE_QUERY_PACKET:	{
				QueryPacket q = new QueryPacket();
				q.function = dis.readInt();
				q.ownerHash = dis.readInt();
				q.imageWidth = dis.readInt();
				q.imageHeight = dis.readInt();
				q.layerCount = dis.readInt();
				q.permissions = dis.readInt();
				q.queryID = dis.readInt();
				
				char[] username = new char[dis.readInt()];
				for(int j = 0; j < username.length; j++)	{
					username[j] = dis.readChar();
				}
				//HEY! This method for conversion maaay be bad!
				q.username = username.toString();
				
				return q;
			}
				
			}
			
		}
		catch(Exception x){
			x.printStackTrace();
		}
		return null;
		
		
	}
}
